﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Galoap.JSON;
using System.Linq;
using Galoap;
using Galoap.Properties;
using Galoap.UnitTest.Storage;
using Galoap.Questions;
using Galoap.UnitTest;

public partial class Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {
        Hashtable lista = new Hashtable();
        string valore, risposta = "";        
        string richiesta = Request.Params[0].ToString();
        try { lista = (Hashtable)JSON.JsonDecode(richiesta); }
        catch (SystemException) { return; }
        try {
            string type = (string)lista["event"];
            switch (type)  {
                case "nickname":  {
                        risposta = "false";
                        valore = (string)lista["value"];
                        try  {
                            Galoap.User.GetUserByNickname(valore);
                        }
                        catch (System.ArgumentException ) {
                            risposta = "true";
                        }
                    }
                    break;
                case "email":  {
                        risposta = "false";
                        valore = (string)lista["value"];
                        try  {
                            Galoap.User.GetUserByEmail(valore);
                        }
                        catch (System.ArgumentException) { 
                            risposta = "true";
                        }
                        
                    }
                    break;
                
                case "registration":    {
                        string nick = (string)lista["nickname"];
                        string firstname = (string)lista["firstname"];
                        string lastname = (string)lista["lastname"];
                        string email = (string)lista["email"];
                        string password = (string)lista["password"];
                        User u = new User { Name = firstname, LastName = lastname, NickName = nick, Email = email, Password = password };
                        Galoap.User.Register(u);
                        risposta = nick;
                    }
                    break;
            }
        }
        catch (NullReferenceException) {
            //serve se no carica pagina vuota
            return;
        }
        Response.Write(risposta);
        Response.End();
    }

}
